<div class="alert-title">发送消息</div>

<table class="message-info">
    <tr>
        <td class="title">显示位置：</td>
        <td class="field">
            <select data-am-selected class="type">
                <option value="首页弹窗">首页弹窗</option>
                <option value="右下弹窗">右下弹窗</option>
                <option value="消息中心">消息中心</option>
            </select>
        </td>
    </tr>
    <tr>
        <td class="title">排序：</td>
        <td class="field">
            <input type="number" class="am-form-field" name="sort" placeholder="序号越大显示越靠前"/>
        </td>
        <td class="copy"></td>
    </tr>

    <tr class="count">
        <td class="title">显示次数：</td>
        <td class="field">
            <label class="am-radio-inline">
                <input type="radio" name="count" value="0" data-am-ucheck> 一次
            </label>
            <label class="am-radio-inline">
                <input type="radio" name="count" value="1" data-am-ucheck checked> 永久
            </label>
        </td>
    </tr>

    <tr>
        <td class="title">标题：</td>
        <td class="field">
            <input type="text" class="am-form-field" name="title" placeholder="请输入消息标题"/>
        </td>
    </tr>

    <tr>
        <td class="title">内容：</td>
        <td class="field">
            <textarea placeholder="请输入消息正文内容" rows="5" style="width: 100%; padding: 5px;" name="content"></textarea>
        </td>
        <td class="copy"></td>
    </tr>

    <tr>
        <td class="title">发送到：</td>
        <td class="field">
            <select data-am-selected class="all-people">
                <option value="1" selected>全员消息</option>
                <option value="0">指定用户</option>
            </select>
            <textarea placeholder="多个用户请用,分割" rows="3" style="width: 100%; padding: 5px;" name="receiver"></textarea>
        </td>
    </tr>
</table>

<div class="button_line add-notice-buttons">
    <button class="btn-save admin_button">确定</button>
    <button class="close admin_button">取消</button>
</div>

<style type="text/css">
    .message-info .title {
        width: 100px !important;
    }
    .message-info {
        width: 100%;
    }
    .message-info td {
        padding: 5px;
    }
</style>

<script>
    $(".add-notice-buttons .close").click(function() {
        $("#add_notice").remove();
    });

    $("textarea[name='receiver']").attr("disabled","disabled");
    $(".message-info .all-people").change(function() {
        var v = $(".message-info .all-people").val();
        if (v == 1) {
            $("textarea[name='receiver']").attr("disabled","disabled");
        }
        else {
            $("textarea[name='receiver']").removeAttr("disabled");
        }
    });

    $(".message-info .type").change(function() {
        var v = $(".message-info .type").val();
        if (v == '首页弹窗') {
            $(".message-info .count").show();
        }
        else {
            $(".message-info .count").hide();
        }
    });

    if (context.center.noticeOper == 'edit') {
        // 填充内容
        var notice = context.center.notice;
        $('.message-info input[name="sort"]').val(notice.sort || 1);
        $(".message-info input[name='title']").val(notice.subject);
        $(".message-info textarea[name='content']").val(notice.content);
        if (notice.count == 9999) {
            // 永久弹
            $(".message-info [value='0']")[0].checked = false;
            $(".message-info [value='1']")[0].checked = true;
        }
        else {
            $(".message-info [value='0']")[0].checked = true;
            $(".message-info [value='1']")[0].checked = false;
        }

        if (!notice.user.id) {
            // 全员消息
            $(".message-info .all-people").val(1);
        }
        else {
            $(".message-info .all-people").val(0);
            $("textarea[name='receiver']").val(notice.user.id);
        }
        console.log(notice);////----
    }

    // 查询用户列表是否都存在
    function checkUserExist(cb) {
        var list = [];
        var arr = $("textarea[name='receiver']").val().split(",");
        var f = function(uid) {
            CMD.find_user_by_name(uid, function(r) {
                if (r.list.length == 0) {
                    stopLoading();
                    alert2("用户" + uid + "不存在！", true);
                    return;
                }
                list.push(r.list[0].id);
                if (list.length >= arr.length) {
                    // 获取完成了
                    cb(list);
                }
            });
        }
        for (var i in arr) {
            f(arr[i]);
        }
    }

    $(".add-notice-buttons .btn-save").click(function() {
        var order = $('.message-info input[name="sort"]').val();
        if (order == '') order = 0;
        var subject = $(".message-info input[name='title']").val().trim();
        if (subject == '') {
            alert2("请输入消息标题！", true);
            return;
        }

        var content = $(".message-info textarea[name='content']").val().trim();
        if (content == '') {
            alert2("请输入消息内容！", true);
            return;
        }
        var type = $(".message-info .type").val();
        var forever = $(".message-info [name='count']:checked").val();
        if (type != '首页弹窗') forever = 0;

        var all = $(".message-info .all-people").val() == 1;
        if (!all && !$("textarea[name='receiver']").val().trim()) {
            alert2("请输入接收消息的用户！", true);
            return;
        }

        startLoading();
        if (all) {
            // 全员消息
            saveOneNotice(null, order, subject, content, type, forever, true);
            return;
        }

        checkUserExist(function(list) {
            for (var i = 0; i < list.length; i++) {
                saveOneNotice(list[i], order, subject, content, type, forever, i == list.length - 1);
            }
        });
    });

    function saveOneNotice(user, order, subject, content, type, forever, last) {
        var data = {
            sort: order,
            subject: subject,
            content: content,
            count: forever? 9999 : 1,
            type: type,
        };
        if (user) data.user = user;

        CMD.notice_save(data, function() {
            if (last) {
                stopLoading();
                $("#add_notice").remove();
                alert2("消息添加成功！");
                if (_f) _f();
            }
        });
    }
</script>